"""
装饰器计算乌兔赛跑案例
"""
import time
import random


def runtime_log(player):
    def runtime(func):
        def inner(*args, **kw):
            print(f"{player}开始奔跑了".center(30,"="))
            start_time = time.perf_counter()
            func(*args, **kw)
            end_time = time.perf_counter()
            print(f">> 选手{player}的最终成绩：{end_time - start_time:.2f} s")
        return inner
    return runtime

@runtime_log("乌龟")
def tortoise(track_length):
    for i in range(1, track_length + 1):
        print(f"乌龟跑了{i}米")
        time.sleep(1)

@runtime_log("兔子")
def rabit(track_length):
    for i in range(1, track_length + 1):
        if i % 4 == 0:
            time.sleep(random.randint(1, 10))
        print(f"兔子跑了{i}米")

if __name__ == "__main__":
    track_len = 10
    tortoise(track_len)
    rabit(track_len)